'''
openpyxl
将数组写入excel指定位置的辅助工具

示例代码：
# 创建一个新工作簿
wb = openpyxl.Workbook()    
# 创建一个新的工作表
wb.create_sheet('NewSheet')
# 定义要写入的数据
data = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9,10]
]
for key,value in array_to_excel_cell(data,5,'d').items():
    wb['NewSheet'][f'{key}']=value
wb.save('example.xlsx')
'''
def number_to_excel_column(n):
    # 把自然数转成A-Z表示的27进制字符，用于转换成excel列
    result = ''
    while n > 0:
        n -= 1
        remainder = n % 26
        current_char = chr(ord('A') + remainder)
        result = current_char + result
        n = n // 26
    return result
def excel_column_to_number(column):
    # 把excel用字母表示的列转成自然数表示的列序号
    if not column:
        return 0
    result = 0
    column = column.upper()
    for char in column:
        if not char.isupper():
            raise ValueError("Column must be uppercase letters only.")
        result = result * 26 + (ord(char) - ord('A') + 1)
    return result
def array_to_excel_cell(data : list,start_col :str = 'A',start_row :int =1):
    start_col = excel_column_to_number(start_col)
    sheet_dict = {}
    for row_idx, row in enumerate(data):
        for col_idx, value in enumerate(row):
            current_row = start_row + row_idx
            current_col = start_col + col_idx
            sheet_dict[f'{number_to_excel_column(current_col)}{current_row}'] = value
    return sheet_dict